import {
	toast
} from '@/utils/common'
import {
	getWxAppId
} from '@/api/sr/wechat'
import {
	AppType
} from '@/utils/constant.js'
export default {
	data() {
		return {
			title: '欢迎光临！', //填写logo或者app名称，也可以用：欢迎回来，看您需求
			code: '', //微信code
		};
	},
	computed: {
		appMp() {
			return uni.getStorageSync('appType') == AppType.MP
		},
		appMINI() {
			return uni.getStorageSync('appType') == AppType.MINI
		}
	},
	onShow() {
		var isMp = uni.getStorageSync('appType') == AppType.MP;
		var isMINI = uni.getStorageSync('appType') == AppType.MINI;
		console.info('login-onload', isMp, isMINI, uni.getStorageSync('isLogin'))
		if (uni.getStorageSync('isLogin')) {
			if (isMp) {
				this.weChatLogin();
			}
		}
		if (!isMINI && !isMp) {
			this.$tab.navigateTo('/pages/login/login')
		}
	},
	methods: {
		toWeChatLogin() {
			this.$modal.loading("登录中，请耐心等待...")
			var that = this;
			const appType = uni.getStorageSync('appType')
			console.info(appType)
			if (appType !== AppType.MP && appType !== AppType.MINI) {
				return
			}
			console.info(uni.getStorageSync('isLogin'))
			if (uni.getStorageSync('isLogin')) {
				return;
			}
			uni.setStorageSync('isLogin', true)
			if (appType === AppType.MINI) {
				// 微信小程序:请求微信接口获取 code
				wx.login({
					success: (res) => {
						// 用code 请求登录
						console.info(res)
						that.code = res.code;
					}
				})
				that.getUserInfo();
			}
			if (appType === AppType.MP) {
				that.weChatLogin();
			}
		},
		getUserInfo() {
			const that = this
			uni.getUserProfile({
				desc: "获取用户信息",
				success(res) {
					console.info('getUserProfile', res)
					that.$store.dispatch('XcxLogin', that.code).then(() => {
						that.$modal.closeLoading()
						uni.setStorageSync('isLogin', false);
						// 设置用户信息
						console.info('getUserProfile-XcxLogin', res)
						that.$store.dispatch('UpdateUserInfo', res).then(resInfo => {
							that.$store.dispatch('getMineBalance');
							that.$store.dispatch('getMineCard');
							that.$tab.switchTab('/pages/user/index')
						}).catch(() => {})
					}).catch(() => {
						that.$modal.closeLoading();
						uni.setStorageSync('isLogin', false);
						that.$tab.reLaunch('/pages/login/index')
					})
				},
				fail(err) {
					that.$modal.closeLoading();
					uni.setStorageSync('isLogin', false);
					console.log(err)
				}
			})
		},
		weChatLogin() {
			const that = this
			const url = window.location.href
			if (url.indexOf('code') > -1) {
				that.$modal.closeLoading();
				uni.setStorageSync('isLogin', false);
				if (getApp().globalData.isLanding) return
				getApp().globalData.isLanding = true
				const code = url.split('code=')[1].split("&")[0];
				that.$store.dispatch('MpLogin', code).then(() => {
					// 设置用户信息
					that.$store.dispatch('GetInfo').then(res => {
						that.$tab.reLaunch('/pages/user/index')
					}).catch(() => {})
				}).catch(() => {
					window.location.href = window.location.origin + '#/pages/login/index'
				})
			} else {
				getWxAppId().then(res => {
					console.info('getWxAppId',res)
					const redirectUrl = window.location.href;
					let param = 'appid=' + res.data;
					param += '&response_type=code'
					param += '&scope=snsapi_base'
					param += '&redirect_uri=' + encodeURIComponent(redirectUrl)
					param += '&state=bihuashop#wechat_redirect'
					window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?' + param
				}).catch(err => {
					window.location.href = window.location.origin + '#/pages/login/index'
				})
			}
		},
		goPage(url) {
			this.$tab.navigateTo(url)
		},
		passwordLogin() {
			this.$tab.navigateTo('/pages/login/passwordLogin')
		},
		todo() {
			toast('开发中');
		},
		getPhoneNumber(e) {
			console.log('getPhoneNumber', e)
		}
	}
};